<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>计算两个字符串的相似度</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sha1.html">« sha1</a></li>
      <li style="float: right;"><a href="function.soundex.html">soundex »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>计算两个字符串的相似度</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.similar-text" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">similar_text</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">similar_text</span> &mdash; <span class="dc-title">计算两个字符串的相似度</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.similar-text-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>similar_text</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string1</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$string2</code></span>, <span class="methodparam"><span class="type">float</span> <code class="parameter reference">&$percent</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   两个字符串的相似程度计算依据 Programming Classics: Implementing the World&#039;s Best Algorithms by Oliver (ISBN 0-131-00413-1)  的描述进行。注意该实现没有使用 Oliver 虚拟码中的堆栈，但是却进行了递归调用，这个做法可能会导致整个过程变慢或变快。也请注意，该算法的复杂度是 O(N**3)，N 是最长字符串的长度。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.similar-text-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string1</code></dt>

     <dd>

      <p class="para">
       第一个字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">string2</code></dt>

     <dd>

      <p class="para">
       第二个字符串。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        交换 <code class="parameter">string1</code> 和 <code class="parameter">string2</code>
        可能会产生不同的结果；请看下面的示例。
       </p>
      </p></blockquote> 
     </dd>

    
    
     <dt>
<code class="parameter">percent</code></dt>

     <dd>

      <p class="para">
       第三个参数通过引用传递，<span class="function"><strong>similar_text()</strong></span> 将以百分比计算相似度，通过将
       <span class="function"><strong>similar_text()</strong></span> 的结果除以指定字符串长度的平均值然后乘以 <code class="literal">100</code>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.similar-text-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回在两个字符串中匹配字符的数量。
  </p>
  <p class="para">
   The number of matching characters is calculated by finding the longest first
   common substring, and then doing this for the prefixes and the suffixes,
   recursively. The lengths of all found common substrings are added.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.similar-text-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="similar_text.example.swapping">
   <p><strong>示例 #1 <span class="function"><strong>similar_text()</strong></span> 交换参数示例</strong></p>
   <div class="example-contents"><p>
    此示例展示了交换 <code class="parameter">string1</code> 和 <code class="parameter">string2</code>
    可能会产生不同的结果。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sim&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #DD0000">'bafoobar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'barfoo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$perc</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"similarity:&nbsp;</span><span style="color: #0000BB">$sim</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$perc</span><span style="color: #DD0000">&nbsp;%)\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sim&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #DD0000">'barfoo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bafoobar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$perc</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"similarity:&nbsp;</span><span style="color: #0000BB">$sim</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$perc</span><span style="color: #DD0000">&nbsp;%)\n"</span><span style="color: #007700">;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
similarity: 5 (71.428571428571 %)
similarity: 3 (42.857142857143 %)
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.similar-text-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.levenshtein.html" class="function" rel="rdfs-seeAlso">levenshtein()</a> - 计算两个字符串之间的 Levenshtein 距离</span></li>
    <li class="member"><span class="function"><a href="function.soundex.html" class="function" rel="rdfs-seeAlso">soundex()</a> - Calculate the soundex key of a string</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>